<?php

namespace App\Http\Middleware;


use App\Jobs\OperationLogJob;
use Closure;
use Illuminate\Support\Facades\Auth;

class AdminLogMiddleware
{
    /**
     * 处理传入的参数
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
       $user =  Auth::guard('admin')->user();//获取用户信息

        // 剔除OPTIONS请求
        if (!in_array($method = $request->getMethod(), ['OPTIONS'])) {
            $data = [
                'admin_id' => Auth::guard('admin')->id(),
                'admin_name'=> $user->real_name,
                'merchant_id'=> $user->merchant_id,
                'path' => $request->getPathInfo(),
                'action' => $method,
                'method' => $request->header("user-agent"),
                'type' => "system",
                'ip' => $request->getClientIp(),
//                'input' => \json_encode($request->all(), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE),
            ];

            // 异步写入，提高操作流畅性
            $job = (new OperationLogJob($data));
            dispatch($job);
        }

        return $next($request);
    }

}
